package com.efast.dmom.equipment.service.masterdata.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.EquipIdsParamBean;
import com.efast.dmom.equipment.bean.ResultBean;
import com.efast.dmom.equipment.dao.masterdata.EquipMasterDataDao;
import com.efast.dmom.equipment.service.masterdata.IEquipMasterDataService;

@Service
public class EquipMasterDataServiceImpl implements IEquipMasterDataService {

	@Autowired
	private EquipMasterDataDao equipMasterDataDao;

	@Override
	public void deleteEquipClassByIds(Integer id) {
		if (id != null && id > 0) {
			equipMasterDataDao.deleteEquipClassByIds(id);
		}

	}

	@Override
	public void deleteEquipCategoryByIds(List<Integer> ids) {
		if (ids.size() > 0) {
			equipMasterDataDao.deleteEquipCategoryByIds(ids);
		}

	}

	@Override
	public void deleteEquipSpareById(List<Integer> ids) {
		if (ids.size() > 0) {
			List<EquipIdsParamBean> beanList = new ArrayList<EquipIdsParamBean>();
			for (Integer id:ids) {
				EquipIdsParamBean equipIdsParamBean = new EquipIdsParamBean();
				equipIdsParamBean.setId(id);
				equipIdsParamBean.setLastUpdateTime(new Date());
				equipIdsParamBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
				beanList.add(equipIdsParamBean);
			}
			equipMasterDataDao.deleteEquipSpareByIds(beanList);
		}

	}

	@Override
	public void deleteEquipBaseInfoByIds(List<Integer> ids) {
		if (ids.size() > 0) {
			List<EquipIdsParamBean> beanList = new ArrayList<EquipIdsParamBean>();
			for (Integer id:ids) {
				EquipIdsParamBean equipIdsParamBean = new EquipIdsParamBean();
				equipIdsParamBean.setId(id);
				equipIdsParamBean.setLastUpdateTime(new Date());
				equipIdsParamBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
				beanList.add(equipIdsParamBean);
			}
			equipMasterDataDao.deleteEquipBaseInfoByIds(beanList);
		}

	}

	@Override
	public ResultBean verifyEquipCode(EquipIdsParamBean bean) {
		ResultBean resultBean = new ResultBean(true, ' ');
		int qty = equipMasterDataDao.getCountByEquipCode(bean.getEquipCode());
		if (qty > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("设备编码不唯一！");
			return resultBean;
		}
		return resultBean;
	}

	@Override
	public ResultBean verifyEquipSpareCode(EquipIdsParamBean bean) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (bean.getSpareCategory().equals("备品备件")) {
			String spareCategory = null;
			int spareCodeQty = equipMasterDataDao.getSpareCodeQty(bean.getSpareCode(), spareCategory);
			if (spareCodeQty > 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("“备品备件”类型的备件编码不唯一！");
				return resultBean;
			}
		} else if (bean.getSpareCategory().equals("刀具")) {
			if (bean.getCutterType().equals("刀具-有编号")) {
				int sparePartQty = equipMasterDataDao.getSpareCodeQty(bean.getSpareCode(), "备品备件");
				if (sparePartQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("存在刀具编号的刀具,其备件编码与“备品备件”类型的备件编码重复！");
					return resultBean;
				}
				int cutterCodeQty = equipMasterDataDao.getCutterCodeQty(bean.getSpareCode(), bean.getCutterCode());
				if (cutterCodeQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("存在刀具编号的刀具,同一备件编码下的刀具编号不唯一！");
				}	
			} else {
				int spareCutterQty = equipMasterDataDao.getSpareCodeQty(bean.getSpareCode(), null);
				if (spareCutterQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("无刀具编号的刀具,备件编码不唯一！");
					return resultBean;
				}
			}
		}
		return resultBean;
	}

	@Override
	public ResultBean spareDeleteVerify(EquipIdsParamBean bean) {
		ResultBean resultBean = new ResultBean(true," ");
		if (bean.getIds() != null && bean.getIds().size() > 0) {
			for (Integer id:bean.getIds()) {
				int inventory = equipMasterDataDao.getInventoryById(id);
				if (inventory > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("备件库存大于0,无法删除！");
					return resultBean;
				}
				int inboundQty = equipMasterDataDao.getInboundQtyBySpareId(id);
				if (inboundQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("该备件被在备件入库中被引用，无法删除！");
					return resultBean;
				}
				int outboundQty = equipMasterDataDao.getOutboundQtyBySpareId(id);
				if (outboundQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("该备件被在备件出库中被引用，无法删除！");
					return resultBean;
				}
				int startWorkQty = equipMasterDataDao.getStartWorkQtyBySpareId(id);
				if (startWorkQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("该备件在产品执行中引用，无法删除！");
					return resultBean;
				}
				int repairInfoQty = equipMasterDataDao.getRepairInfoQtyBySpareId(id);
				if (repairInfoQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("该产品在产品执行中被引用，无法删除！");
					return resultBean;
				}
			}	
		}
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean equipDeleteVerify(EquipIdsParamBean bean) {
		ResultBean resultBean = new ResultBean(true," ");
		if (bean.getIds() != null && bean.getIds().size() > 0) {
			for (Integer id:bean.getIds()) {
				int startWorkQty = equipMasterDataDao.getStartWorkQtyByEquipId(id);
				if (startWorkQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("该设备在产品执行中被引用，无法删除！");
					return resultBean;
				}
				int repairInfoQty = equipMasterDataDao.getRepairInfoQtyByEquipId(id);
				if (repairInfoQty > 0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("该设备在设备维修中被引用，无法删除！");
					return resultBean;
				}
			}
		}
		resultBean.setSuccess(true);
		return resultBean;
	}
}
